/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultarEmprestimo.java
 *
 * Created on 12/12/2011, 00:45:04
 */
package gui;

import DAO.DAOGenerico;
import DAO.DAOJSON;
import empregado.Empregado;
import emprestimo.Emprestimo;
import emprestimo.EmprestimoService;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author arruda
 */
public class ConsultarEmprestimo extends javax.swing.JDialog {

    DAOGenerico daoConsultar;
    EmprestimoService emprestimoService;
    /** Creates new form ConsultarEmprestimo */
    public ConsultarEmprestimo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        daoConsultar = new DAOJSON();
        emprestimoService = new EmprestimoService();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        emprestimos_txt = new javax.swing.JTextArea();
        constultarBt = new javax.swing.JButton();
        txtcpf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        emprestimos_txt.setColumns(20);
        emprestimos_txt.setEditable(false);
        emprestimos_txt.setRows(5);
        jScrollPane1.setViewportView(emprestimos_txt);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 561, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        constultarBt.setText("Consultar");
        constultarBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                constultarBtActionPerformed(evt);
            }
        });

        txtcpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcpfActionPerformed(evt);
            }
        });
        txtcpf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtcpfFocusLost(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(0, 51, 51));
        jLabel2.setText("Procurar cpf:");

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Consultar Empréstimo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtcpf, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                        .addComponent(constultarBt)
                        .addGap(117, 117, 117))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtcpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(constultarBt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtcpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcpfActionPerformed
  
}//GEN-LAST:event_txtcpfActionPerformed

private void txtcpfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtcpfFocusLost

}//GEN-LAST:event_txtcpfFocusLost
private void printEmprestimos(ArrayList<Emprestimo> emprestimos){
    String texto = "";
    for(Emprestimo emprestimo : emprestimos){
        System.out.println("emprestimo"+emprestimo.getId());
//        String temp = "Emprestimo: "+emprestimo.getValorDesejado();
        texto+="==============================================\n";
        System.out.println(texto);
        texto+="Emprestimo: "+emprestimo.getValorDesejado()+" ";
        texto+="Prestações: "+emprestimo.getNumPrestacoes()+" ";
        texto+="Valor das Prestacoes: "+emprestimo.getPrestacao()+" ";
        texto+="Juros: "+emprestimo.getJuros()+" ";
        texto+="\n==============================================\n";
        System.out.println(texto);
    }
    this.emprestimos_txt.setText(texto);
}

private void constultarBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_constultarBtActionPerformed
    Empregado emp = new Empregado();
    emp.setCpf(txtcpf.getText());
    try {
        //ValidateAnnotationParser validar = new ValidateAnnotationParser();
        //if(validar.validate(empr)){
           Empregado empBusca = (Empregado) daoConsultar.buscar(emp);
           if(empBusca == null){
               JOptionPane.showMessageDialog(this,"CPF invalido!", "Erro", JOptionPane.ERROR_MESSAGE);
               return;
           }
        //}
    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        return;
    }
    
    
    ArrayList<Emprestimo> emprestimos = emprestimoService.consultarEmprestimos(emp);
    printEmprestimos(emprestimos);
    return;
}//GEN-LAST:event_constultarBtActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConsultarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConsultarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConsultarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConsultarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ConsultarEmprestimo dialog = new ConsultarEmprestimo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton constultarBt;
    private javax.swing.JTextArea emprestimos_txt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtcpf;
    // End of variables declaration//GEN-END:variables
}
